<?php

class Service_bing extends MY_Service
{
    public function __construct()
    {
        parent::__construct();
        $this->load->config('common/bing');
        $this->load->service('Service_configs');
        $this->load->library('common/CommonEnum');
    }

    public function indexNow($url)
    {
        $siteConfig = json_decode($this->Service_configs->getConfigVal(CommonEnum::SITE_CONFIG_ID, true), true);

        $site = $this->config->item('IndexNow');
        $site['data']['host'] = $siteConfig['hosts'];
        $site['data']['key'] = $siteConfig['bing']['index_now']['key'];
        $site['data']['keyLocation'] = "https://" . $siteConfig['hosts'] . "/" . $siteConfig['bing']['index_now']['key'] . ".txt";
        $site['data']['urlList'][] = $url;

        $curl = curl_init();
        curl_setopt_array($curl, array(
            CURLOPT_URL => $site['url'],
            CURLOPT_RETURNTRANSFER => true,
            CURLOPT_ENCODING => '',
            CURLOPT_MAXREDIRS => 10,
            CURLOPT_TIMEOUT => 1,
            CURLOPT_FOLLOWLOCATION => true,
            CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
            CURLOPT_CUSTOMREQUEST => 'POST',
            CURLOPT_POSTFIELDS => json_encode($site['data']),
            CURLOPT_HTTPHEADER => array(
                'Content-Type: application/json'
            ),
        ));

        $response = curl_exec($curl);
        curl_close($curl);
        return $response;
    }
}